import React, {Component} from 'react'
import {Route, withRouter} from 'react-router-dom'
import AnimatedRouter from './components/animated_router'
import routes from './routes'
import {setServers} from './actions'
import * as R from 'ramda'

function handleServer(arr) {
  arr.forEach(item => {
    if (item.opt_data_array.length) {
      item.opt_data_array = R.dropRepeatsWith((a, b) => a.v === b.v)(item.opt_data_array)
      handleServer(item.opt_data_array)
    } else {
      delete item.opt_data_array
    }
  })
}

@withRouter
class App extends Component {

  componentWillReceiveProps() {}

  componentWillMount() {
    let STD_DATA = window.BNSServerSelect.STD_DATA
    STD_DATA = R.dropRepeatsWith((a, b) => a.v === b.v)(STD_DATA)
    handleServer(STD_DATA)
    this.props.store.dispatch(setServers(STD_DATA))
  }

  shouldComponentUpdate() {
    return true
  }

  render() {
    return (
      <AnimatedRouter>
        {
          routes.map(({name, path, exact = true, component}, key) => (
            <Route path={path} exact={exact} component={component} key={key} />
          ))
        }
      </AnimatedRouter>
    )
  }
}

export default App
